package com.education.wisdom.controller.app;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.core.BaseController;
import com.education.common.core.TableDataInfo;
import com.education.common.poi.ExcelUtil;
import com.education.common.pojo.AjaxResult;
import com.education.common.utils.LogUtil;
import com.education.wisdom.pojo.Coupons;
import com.education.wisdom.service.ICouponsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 优惠券 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-04-01
 */
@Controller
@Api(tags = {"优惠券 信息操作处理"})
@CrossOrigin
@Slf4j
@RequestMapping("/appCoupons")
public class AppCouponsController extends BaseController
{
    private String prefix = "system/coupons";
	
	@Autowired
	private ICouponsService couponsService;
	
	@GetMapping()
	public String coupons()
	{
	    return prefix + "/coupons";
	}
	
	@GetMapping("/listByUserId")
	@ResponseBody
	@ApiOperation("优惠券列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNo",value = "当前页", dataType = "Integer"),
			@ApiImplicitParam(name = "userId",value = "创建人", dataType = "String"),
			@ApiImplicitParam(name = "yhType",value = "1课程优惠券2平台优惠券", dataType = "String")
			})
	public void listByUserId(HttpServletResponse response, HttpServletRequest request, String userId, String yhType, Integer pageNo) throws Exception{
			Integer pageSize = 10;	
			log.info(LogUtil.LogInfo("优惠券列表",new Date()));
			PageHelper.startPage(pageNo.intValue(),pageSize.intValue());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
			map.put("yhType", yhType);
			List<Coupons> list = couponsService.selectCouponsListByUserId(map);
			for (int i = 0; i < list.size(); i++) {
				Date endTime = list.get(i).getEndTime();
				Date date = new Date();
				//比较时间，判断代金券是否过期
				if (date.before(endTime)) {
					list.get(i).setIsOverdue("1");
				}else {
					list.get(i).setIsOverdue("2");
				}
			}
			writeJSON(new PageInfo<Coupons>(list), response, "200", "成功");

	}
	
	/**
	 * 查询优惠券列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Coupons coupons)
	{
		startPage();
        List<Coupons> list = couponsService.selectCouponsList(coupons);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出优惠券列表
	 */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Coupons coupons)
    {
    	List<Coupons> list = couponsService.selectCouponsList(coupons);
        ExcelUtil<Coupons> util = new ExcelUtil<Coupons>(Coupons.class);
        return util.exportExcel(list, "coupons");
    }
	
	/**
	 * 新增优惠券
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存优惠券
	 */
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Coupons coupons)
	{		
		return toAjax(couponsService.insertCoupons(coupons));
	}

	/**
	 * 修改优惠券
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Integer id, ModelMap mmap)
	{
		Coupons coupons = couponsService.selectCouponsById(id);
		mmap.put("coupons", coupons);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存优惠券
	 */
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Coupons coupons)
	{		
		return toAjax(couponsService.updateCoupons(coupons));
	}
	
	/**
	 * 删除优惠券
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(couponsService.deleteCouponsByIds(ids));
	}
	
}
